import smartfetch from '@mmstudio/an000058';
import React from 'react';
import { message } from 'antd';
import api from '../../../atoms/api';
import DelButton from '../../../components/delbutton';
import Table from '../../../components/table';
import { IData } from '../../api/admin/users/query.api';
import { Message as M1, Result as R1 } from '../../api/account/resetpsw.api';
import Edit from './edit';
import IconButton from '../../../components/iconbutton';
import confirm from '../../../atoms/confirm';

export default function List({ data }: { data: IData[]; }) {
	return <>
		<Table
			idfieldname='userid'
			data={data}
			render={(record) => {
				const userid = record.userid;
				return <>
					<Edit data={record} />
					<DelButton query={{
						userid
					}} dlgtitle='确认删除' api={api['/api/admin/users/delete']} />
					<IconButton img='/icons/psw.svg' label='重置密码' onClick={async () => {
						const ok = await confirm('重置', '确认重置?');
						if (ok) {
							const data = await smartfetch<R1, M1>(api['/api/account/resetpsw'], 'post', {
								userid
							});
							if (data.ok === true) {
								message.success({ content: '操作成功' });
							} else {
								message.error({ content: data.message });
							}
						}
					}} />
				</>;
			}}
		>
			<Table.Column<IData> title='用户名' key='userid' dataIndex='userid' align='center' />
			<Table.Column<IData> title='姓名' key='name' dataIndex='name' align='center' />
			<Table.Column<IData> title='性别' key='sex' dataIndex='sex' align='center' render={(v) => {
				switch (v) {
					case 1:
						return '男';
					case 2:
						return '女';
					case 0:
					default:
						return '未知';
				}
			}} />
			<Table.Column<IData> title='手机号' key='phone' dataIndex='phone' align='center' />
			<Table.Column<IData> title='邮箱' key='email' dataIndex='email' align='center' />
		</Table>
	</>;
}
